from typing import List
from sqlalchemy.orm import Session

from app.dao.model.storage import Storage


def create_storage(db: Session, *, storage: Storage = None, storages: List[Storage] = None):
    if storages is not None:
        db.add_all(storages)
    if storage is not None:
        db.add(storage)

    db.commit()


def get_storage(db: Session, *, page=1, size=20) -> List[Storage]:
    return db.query(Storage).offset((page-1)*size).limit(size).all()


def get_storage_count(db: Session) -> int:
    return db.query(Storage).count()


def delete_storage_by_service_id(db: Session, service_id: str) -> int:
    storage = db.query(Storage).\
        filter(Storage.service_id == service_id).\
        first()
    db.delete(storage)
    db.commit()
